<?php

namespace Database\Seeders;

use Illuminate\Database\Seeder;
use Illuminate\Support\Facades\DB;

class SupplierMenuSeeder extends Seeder
{
    /**
     * Run the database seeds.
     *
     * @return void
     */
    public function run()
    {
        // 查找自提点管理的父菜单
        $parentMenu = DB::table('admin_menu')->where('title', '自提点管理')->first();
        
        if ($parentMenu) {
            // 检查供应商清单菜单是否已存在
            $existingMenu = DB::table('admin_menu')
                ->where('parent_id', $parentMenu->id)
                ->where('title', '供应商清单')
                ->first();
            
            if (!$existingMenu) {
                // 获取最大排序值
                $maxOrder = DB::table('admin_menu')->max('order') ?? 0;
                
                // 添加供应商清单菜单
                DB::table('admin_menu')->insert([
                    'parent_id' => $parentMenu->id,
                    'order' => $maxOrder + 1,
                    'title' => '供应商清单',
                    'icon' => 'fa-users-cog',
                    'uri' => '/suppliers',
                    'created_at' => now(),
                    'updated_at' => now(),
                ]);
                
                $this->command->info('供应商清单菜单已成功添加！');
            } else {
                $this->command->info('供应商清单菜单已存在，跳过添加。');
            }
        } else {
            $this->command->error('未找到自提点管理父菜单，请先运行 PickupManagementMenuSeeder');
        }
    }
}